﻿Imports System.Data.OleDb

Public Class frmThemNhom


    Private Sub cmdLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLuu.Click
        Try
            Dim dbConn As New OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            If NhomId = -1 Then
                dbCommand = New OleDbCommand("spNhom_Add", dbConn)
                dbCommand.CommandType = CommandType.StoredProcedure
                dbCommand.Parameters.Add("@TenNhom", OleDbType.VarWChar).Value = txtTenNhom.Text.Trim()
                dbCommand.Parameters.Add("@GhiChu", OleDbType.VarWChar).Value = txtGhiChu.Text.Trim()
                dbCommand.Parameters.Add("@NguoiTao", OleDbType.VarWChar).Value = NguoiTao()
                dbCommand.Parameters.Add("@NgayTao", OleDbType.VarWChar).Value = NgayTao()
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
            Else
                dbCommand = New OleDbCommand("spNhom_Update", dbConn)
                dbCommand.CommandType = CommandType.StoredProcedure
                dbCommand.Parameters.Add("@NhomId", OleDbType.Integer).Value = NhomId
                dbCommand.Parameters.Add("@TenNhom", OleDbType.VarWChar).Value = txtTenNhom.Text.Trim()
                dbCommand.Parameters.Add("@GhiChu", OleDbType.VarWChar).Value = txtGhiChu.Text.Trim()
                dbCommand.Parameters.Add("@NguoiSua", OleDbType.VarWChar).Value = NguoiTao()
                dbCommand.Parameters.Add("@NgaySua", OleDbType.VarWChar).Value = NgayTao()
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
                txtTenNhom.Text = ""
                txtGhiChu.Text = ""
                Me.Close()
            End If
            txtTenNhom.Text = ""
            txtGhiChu.Text = ""
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHuyBo.Click
        Try
            txtTenNhom.Text = ""
            txtGhiChu.Text = ""
            NhomId = -1
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub
    Public NhomId As Integer = -1
    Private Sub frmThemNhomSanPham_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.BackColor = Color.FromArgb(194, 217, 247)
            txtTenNhom.Text = ""
            txtGhiChu.Text = ""
            If NhomId > 0 Then
                Nhom_LoadItem(NhomId)
            End If
        Catch ex As Exception

        End Try
    End Sub
    Protected Sub Nhom_LoadItem(ByVal NhomId As Integer)
        Try
            Dim dtNhom As New DataTable
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim adapter As OleDb.OleDbDataAdapter
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            dbCommand = New OleDbCommand("spNhom_GetItem", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@NhomId", OleDbType.Integer).Value = NhomId
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtNhom)
            dbConn.Close()
            txtTenNhom.Text = dtNhom.Rows(0)("TenNhom")
            txtGhiChu.Text = dtNhom.Rows(0)("GhiChu")
        Catch ex As Exception

        End Try
    End Sub
End Class